local dir `c(pwd)'
local g=`1'
qui {
clear
set obs 1
foreach type in original order structure {
gen entropy_`type'_convergence=.
gen words_`type'_convergence=.
gen chars_`type'_convergence=.
}
gen book=.             
gen str translation=""	                        
save  entropy_bible_`g', replace

use calculation_`g', clear

local till=_N
local counter=_N

forvalues i=1/`till' {
	use calculation_`g', clear
	local translation=translation[`i']
	local counter=`counter'-1
	use "`dir'\\corpus\\`translation'.dta" , clear
	drop if sentence==""
	
	/* get bible books */
	gen book=real(usubstr(vid,1,2))
	keep if book==40|book==41|book==42|book==43|book==44|book==66
	drop if book==.
	keep book
	
	
	/* create macro containing each available book */

	gen match=1
	collapse (sum) match, by(book)
	local to=_N
	local books
	forvalues b=1/`to' {
		local add=book[`b']
		local books `"`books' `add'"'
		}	
	noisily di "`translation' (left: `counter') "
    	
	foreach book of local books		{			
		foreach type in original order structure {					
			use `"`dir'\\stata_files\length_`translation'_`book'_`type'"', clear
			/* convergence test with stronger condition: only keep first 50% of string */
			drop if _n>.50*_N
			local chars_`type'_`book'=_N
			gen words=1 if v1==" "
			sum words if words==1
			local words_`type'_`book'=r(N)
			ren v2 li
			gen i=_n
			local n=_N
			gen tosum=(li)/(log(i)/log(2))
			sum tosum
			local entropy_`type'_`book'=r(sum)
		}
	}
	
	foreach book of local books		{			
		use  entropy_bible_`g', clear
					local new=_N+1
					set obs `new'
					local in `"in `new'"'
					foreach type in original order structure {
							replace entropy_`type'_convergence=`entropy_`type'_`book'' 	`in'
							replace chars_`type'_convergence=`chars_`type'_`book'' 			`in'
							replace words_`type'_convergence=`words_`type'_`book'' 			`in'
							}   
					replace book=`book'                     			`in'      		
					replace translation="`translation'"	          	`in'  
					save  entropy_bible_`g', replace
				}
			}
/* generate finished file */
clear
set obs 1
gen v=1
save finished_`g', replace
}

clear         
exit, STATA 	
